<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top: 15px">
      <el-button type="primary" @click="dialogFormVisible = true"
        >添加角色</el-button
      >
      <el-table border :data="tableData" style="width: 100%; margin-top: 15px">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <div v-if="scope.row.children.length > 0">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-row
                  v-for="item1 in scope.row.children"
                  :key="item1.id"
                  class="borderBtm_1px"
                >
                  <el-col :span="6">
                    <div
                      class="expand_box"
                      v-if="scope.row.children.length > 0"
                    >
                      <el-tag
                        closable
                        @close="dels(scope.row, scope.row.id, item1.id)"
                      >
                        {{ item1.authName }}
                      </el-tag>
                      <i class="el-icon-caret-right"></i>
                    </div>
                  </el-col>
                  <el-col :span="18">
                    <div>
                      <el-row
                        v-for="item2 in item1.children"
                        :key="item2.id"
                        class="borderBtm_1px"
                      >
                        <el-col :span="6">
                          <div two>
                            <el-tag
                              closable
                              type="success"
                              class="stree"
                              style="pading-top: 50px"
                              @close="dels(scope.row, scope.row.id, item2.id)"
                            >
                              {{ item2.authName }}
                            </el-tag>
                            <i class="el-icon-caret-right"></i>
                          </div>
                        </el-col>
                        <el-col :span="18">
                          <div class="two">
                            <el-tag
                              closable
                              :span="18"
                              v-for="item3 in item2.children"
                              :key="item3.id"
                              type="warning"
                              style="margin-right: 10px; margin-top: 10px"
                              @close="dels(scope.row, scope.row.id, item3.id)"
                            >
                              {{ item3.authName }}
                            </el-tag>
                          </div>
                        </el-col>
                      </el-row>
                    </div>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div v-else>该角色暂无权限</div>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName">
        </el-table-column>
        <el-table-column  label="角色描述" prop="roleDesc">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="emit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row.id)"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="dialogVisible2(scope.row)"
              >权限分配</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加from弹框 -->
    <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        label-width="100px"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="ruleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="ruleForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisibleadd('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 修改角色from弹框 -->
    <el-dialog title="修改角色" :visible.sync="dialogFormVisible1">
      <el-form
        :model="ruleForm"
        label-width="100px"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="ruleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="ruleForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisibleemit('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog title="权限分配" :visible.sync="dialogVisible" width="50%">
      <el-tree
        :data="treeList"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="checkArr"
        :default-expanded-keys="checkArr"
        :props="defaultProps"
        ref="treeRef"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisibleadd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getRoleAPI,
  getRoleAddAPI,
  getRoleDelApi,
  getRoleEmitApi,
  getRoleDellApi,
  getPermissionsAPI,
  getRolesqlApi,
} from "@/http/api";
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogVisible: false,
      ruleForm: {
        roleName: "",
        roleDesc: "",
      },
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
      defaultProps: {
        children: "children",
        label: "authName",
      },
      treeList: [],
      checkArr: [],
    };
  },
  components: {},
  created() {
    this.getrolelist();
  },
  mounted() {},
  methods: {
    dialogVisibleadd() {
      this.checkArr = [];
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ];
      const idStr = keys.join(",");
      getRolesqlApi(idStr, this.id).then((res) => {
        console.log(res);
      });
      this.getrolelist();
      this.dialogVisible = false;
    },
    getcheckid(row, checkArr) {
      // console.log(checkArr,11);

      if (!row.children) {
        return checkArr.push(row.id);
      }

      row.children.forEach((item) => {
        this.getcheckid(item, checkArr);
      });
    },

    async dialogVisible2(row) {
      console.log(row, "row");
      this.id = row.id;
      this.dialogVisible = true;
      var res = await getPermissionsAPI("tree");
      this.treeList = res;
      this.getcheckid(row, this.checkArr);
    },
    async dels(row, roleid, rightid) {
      var res = await getRoleDellApi(roleid, rightid);
      //   this.getrolelist()
      row.children = res;
    },
    async dialogFormVisibleemit() {
      var res = await getRoleEmitApi(this.ruleForm);
      //   console.log(res);
      this.dialogFormVisible1 = false;
      this.getrolelist();
    },
    emit(row) {
      this.dialogFormVisible1 = true;
      // console.log(row);
      this.ruleForm = row;
      console.log(this.ruleForm);
    },
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getRoleDelApi(id).then((res) => {
            this.getrolelist();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async dialogFormVisibleadd() {
      var res = await getRoleAddAPI(this.ruleForm).then((res) => {});
      this.dialogFormVisible = false;
      this.getrolelist();
      this.ruleForm.roleName = "";
      this.ruleForm.roleDesc = "";
    },
    async getrolelist() {
      var res = await getRoleAPI();
      //   console.log(res, 1111);
      this.tableData = res;
    },
  },
  computed: {},
  watch: {},
};
</script>

<style >
/* .el-tag {
  margin: 10px 20px;
}
.expand_box {
  padding: 0 40px;
}
.borderBtm_1px {
  border-bottom: 1px solid #eee;
} */
</style>
